package com.pms.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pms.domain.AuthticateEntity;
import com.pms.domain.AuthticateResEntity;
import com.pms.domain.Operator;
import com.pms.domain.ReqInfo;
import com.pms.service.AuthenService;
import com.pms.service.OperatorService;
import com.pms.utils.CreateResponse;
import com.pms.utils.GetIPAddr;

/**
 * @ClassName: OperatorController
 * @Description: 运营商控制器
 * @author ZZW
 * @date 2016年12月2日 下午5:57:14
 * 
 */
@Controller
@RequestMapping("/operator")
public class OperatorController {
	
	private static final String XML_VIEW_NAME = "marshallingView";   // 返回XML格式的数据
	private static final String JSON_VIEW_NAME = "jsonView";         // 返回JSON格式的数据
	
	private Operator op;
	
	private List<Operator> operatorList;
	
	@Autowired
	private OperatorService operatorService;

	@Autowired
	private AuthenService authenService;

	/**
	 * 取得所有对象
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/query.pfv", method={RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public ModelAndView getOperators(@ModelAttribute ReqInfo reqInfo,
			HttpServletRequest req, HttpServletResponse res) throws IOException {
		AuthticateEntity authenEnt = this.authenService
				.getAuthEntity(reqInfo.getAccount());
		String md5key = "";
		int status = -1;
		// 查询账号存在
		if (authenEnt != null) {
			md5key = authenEnt.getMd5key();
			String signstr = reqInfo.getAccount()
					+ reqInfo.getTs() + md5key;
			String md5Str = DigestUtils.md5Hex(signstr);
			// 验证签名
			if (!md5Str.equals(reqInfo.getSign())
					|| reqInfo.getAccount().equals("")
					|| reqInfo.getTs().equals("")) {
				status = -1;
			} else {
				reqInfo.setIpFrom((GetIPAddr.getIpAddr(req)));
				operatorList = operatorService.getAll();
				if(!operatorList.isEmpty()){
					status = 0;
				} else {
					status = 1;
				}
			}
		} else {
			status = -1;
		}
		AuthticateResEntity authticateResEntity = new AuthticateResEntity();
		if (status == 0) {
			authticateResEntity.setStatus("0");
			authticateResEntity.setStatusDesc("调用成功");
			authticateResEntity.setData(operatorList);
		} else if (status == -1) {
			authticateResEntity.setStatus("-1");
			authticateResEntity.setStatusDesc("sign效验失败");
		}else if (status == 1) {
			authticateResEntity.setStatus("1");
			authticateResEntity.setStatusDesc("无数据");
		} 
		return new ModelAndView(JSON_VIEW_NAME, "object", CreateResponse.createReponse(status, authticateResEntity));
	}

	/**
	 * 获取单个对象
	 * @param operatorType
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/getByType.pfv", method={RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ModelAndView getOperatorByType(
			@RequestParam(value = "operatorType") String operatorType,@ModelAttribute ReqInfo reqInfo,
			HttpServletRequest req, HttpServletResponse res) throws IOException {
		AuthticateEntity authenEnt = this.authenService
				.getAuthEntity(reqInfo.getAccount());
		String md5key = "";
		int status = -1;
		// 查询账号存在
		if (authenEnt != null) {
			md5key = authenEnt.getMd5key();
			String signstr = reqInfo.getAccount()
					+ reqInfo.getTs() + md5key;
			String md5Str = DigestUtils.md5Hex(signstr);
			// 验证签名
			if (!md5Str.equals(reqInfo.getSign())
					|| reqInfo.getAccount().equals("")
					|| reqInfo.getTs().equals("")) {
				status = -1;
			} else {
				reqInfo.setIpFrom((GetIPAddr.getIpAddr(req)));
				op = operatorService.selectByType(Integer.parseInt(operatorType));
				if(op != null){
					if(op.getOperatorName() != null){
						status = 0;
						} else {
							status = 1;
						}
					} else {
						status = 2;
					}
				}
				} else {
					status = -1;
				}
		AuthticateResEntity authticateResEntity = new AuthticateResEntity();
		if (status == 0) {
			authticateResEntity.setStatus("0");
			authticateResEntity.setStatusDesc("调用成功");
			authticateResEntity.setData(op);
		} else if (status == -1) {
			authticateResEntity.setStatus("-1");
			authticateResEntity.setStatusDesc("sign效验失败");
		}else if (status == 1) {
			authticateResEntity.setStatus("1");
			authticateResEntity.setStatusDesc("无数据");
		} else {
			authticateResEntity.setStatus("2");
			authticateResEntity.setStatusDesc("输入参数有误");
		}
		return new ModelAndView(XML_VIEW_NAME, "object", CreateResponse.createReponse(status, authticateResEntity));
	}
	
		

	/**
	 * 插入
	 * @param operator
	 * @param req
	 * @param res
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/insert.pfv", method={RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public ModelAndView insertOperator(@ModelAttribute ReqInfo reqInfo,
			HttpServletRequest req, HttpServletResponse res) throws IOException {
		AuthticateEntity authenEnt = this.authenService
				.getAuthEntity(reqInfo.getAccount());
		String md5key = "";
		int status = -1;
		// 查询账号存在
		if (authenEnt != null) {
			md5key = authenEnt.getMd5key();
			String signstr = reqInfo.getAccount()
					+ reqInfo.getTs() + md5key;
			String md5Str = DigestUtils.md5Hex(signstr);
			// 验证签名
			if (!md5Str.equals(reqInfo.getSign())
					|| reqInfo.getAccount().equals("")
					|| reqInfo.getTs().equals("")) {
				status = -1;
			} else {
				reqInfo.setIpFrom((GetIPAddr.getIpAddr(req)));
				Operator operator = new Operator();
				operator.setOperatorName("联通");
				operator.setOperatorType(2);
				operatorService.insertOperator(operator);
				status = 0;
			}
		}
		AuthticateResEntity authticateResEntity = new AuthticateResEntity();
		if (status == 0) {
			authticateResEntity.setStatus("0");
			authticateResEntity.setStatusDesc("调用成功");
		} else if (status == -1) {
			authticateResEntity.setStatus("-1");
			authticateResEntity.setStatusDesc("sign效验失败");
		}
		return new ModelAndView(XML_VIEW_NAME, "object", CreateResponse.createReponse(status, authticateResEntity));
	}
	

	@RequestMapping(value = "/update.pfv", method = {
			RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void updateOperator(
			@RequestParam(value = "operator") Operator operator)
			throws IOException {
		operatorService.updateByType(operator);
	}

	@RequestMapping(value = "/delete.pfv", method = {
			RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void deleteOperatory(
			@RequestParam(value = "operatorType") Integer operatorType)
			throws IOException {
		operatorService.deleteOperator(operatorType);
	}

}
